<?php
namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\common\model\MemberModel;
use app\common\model\RechargeModel;
use common\util\ArrayUtil;
use app\api\model\Member;

class Stats extends BaseController {

    public function _initialize(){
        $this->needLogin();
    }

    public function team_recharge(){
        
        $date = $this->request->param('date',date('Y-m-d'));
        
        $date = date('Ymd',strtotime($date));
        
        $mid  = $this->getLoginId();
        //获取我所在的团队
        $mids = MemberModel::instance()->getFMemberIdsByMid($mid);
        
        $mids[] = $mid;
        $where = [];//制定查询日期的查询条件
        $monthWhere = [];//月统计的查询条件
        $allWhere = [];//全部的统计查询条件
        
        $where['ifpass'] =1;
        $where['date'] = $date;
        $where['mid'] = ['in',$mids];
        
        $monthWhere['ifpass'] = 1;
        $monthWhere['mid'] = ['in',$mids];
        $monthWhere['date'] = [ 'between',[date('Ym01',time()), date('Ymd', strtotime(date('Y-m-01') . '+1 month -1 day'))]];

        $allWhere['ifpass']= 1;
        $allWhere['mid']= ['in',$mids];;
        
        //月金额
        $monthPrice = RechargeModel::instance()->where($monthWhere)->sum('money');
        //总金额
        $totalPrice =  RechargeModel::instance()->where($allWhere)->sum('money');
        
        //选中时间的记录
        $items = RechargeModel::instance()->where($where)->order('id','desc')->select();
        $allMids = ArrayUtil::getKeyValuesFromArray($items, 'mid');
        $memMap = Member::instance()->getItemsByIds($allMids,'name,face');
        
        $list = [];
        $dayPrice = 0;//选中时间当天的总额
        foreach ($items as $item){
            $list [] = [
                'id'=>$item['id'],
                'name'=>$memMap[$item['mid']]['name'],
                'avatar'=> get_image($memMap[$item['mid']]['face']),
                'money'=>$item['money'],
            ];
            $dayPrice += $item['money'];
        }
        
        $data = [
            'day'=>$dayPrice,
            'month'=>$monthPrice,
            'total'=>$totalPrice,
            'list' => $list,
        ];
        return $this->returnResult(1,'',$data);
    }
}